package com.jhh.pay.haier.service.impl;

import com.jhh.pay.channel.pojo.ResponseBean;
import com.jhh.pay.haier.util.haierUtils.Core;
import com.jhh.pay.haier.util.haierUtils.HaierConstants;
import com.jhh.pay.haier.util.haierUtils.HttpClientUtilsExt;
import com.jhh.pay.haier.util.haierUtils.MapUtils;
import com.loan_utils.util.PropertiesReaderUtil;
import com.netfinworks.mag.security.KJTEncryptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 通用方法类
 */
public class HaierBaseServiceImpl {

    private String gatewayUrl = PropertiesReaderUtil.read("haier", "gatewayUrl");

    private String fourAuthURL = PropertiesReaderUtil.read("haier", "fourAuthURL");

    private static final Logger logger = LoggerFactory.getLogger(HaierBaseServiceImpl.class);

    //实例化加密类
    KJTEncryptService kjtEncryptService = new KJTEncryptService();

    /**
     * 判断是否请求成功
     *
     * @param noteResult
     */
    public ResponseBean is_success(Object obj) {
        //将参数放入map
        Map<String, String> sParaTemp = MapUtils.setConditionMap(obj);
        ResponseBean resBean;
       try {
           //参数加密
           Map<String, String> map = Core.buildRequestPara(sParaTemp, "");
           //发送post请求
           String response = HttpClientUtilsExt.post(gatewayUrl, map, map.get("_input_charset"));
           //返回格式转换
           Map<String, String> result = MapUtils.stringToMap(response, "");
           logger.info("--------------------->>>>>>>>调用海尔三方响应参数--------------<<<<<<<<<\nresponse=" + response + "\nresult=" + result);
           //判断调用是否成功
           String is_success = result.get("is_success");

           if (HaierConstants.SUCCESS.equals(is_success)) {
               resBean = ResponseBean.SUCCESS_RESPONSE();
               resBean.setMsg(HaierConstants.SUCCESS_VALUE);
           } else if (HaierConstants.DEAL.equals(is_success)) {
               resBean = ResponseBean.WAIT_RESPONSE();
               resBean.setMsg(result.get("memo") != null ? result.get("memo") : result.get("error_message"));
           } else {
               resBean = ResponseBean.FAIL_RESPONSE();
               resBean.setMsg(result.get("memo") != null ? result.get("memo") : result.get("error_message"));
           }
       }catch (Exception e){
           logger.error("调用第三方异常：具体内容----------------",e);
           resBean = ResponseBean.SYSERROR_RESPONSE();
           return resBean;
       }
        return resBean;
    }

    /**
     *  查询请求第三方
     * @param
     */
    public Map post(Object obj,String type) throws Exception{
        //将参数放入map
        Map<String, String> sParaTemp = MapUtils.setConditionMap(obj);
        //参数加密
        Map<String, String> map = Core.buildRequestPara(sParaTemp,"");
        //发送post请求
        String response = HttpClientUtilsExt.post(gatewayUrl,map,map.get("_input_charset"));
        //返回格式转换 type判断是代扣还是代付
        Map<String,String> result = MapUtils.stringToMap(response,type);
        logger.info("--------------------->>>>>>>>调用三方响应参数--------------<<<<<<<<<\nresponse="+response+"\nresult="+result);
        return result;

    }

    /**
     *  判断代扣否请求成功
     */
    public ResponseBean trade_success(Map<String,String> result){
        ResponseBean resBean;
        String is_success = result.get("is_success");
        if (HaierConstants.SUCCESS.equals(is_success)){
            //判断订单是否受理成功
            if (HaierConstants.TRADE_FINISHED.equals(result.get("tradelist"))){ //成功
                resBean = ResponseBean.SUCCESS_RESPONSE();
                resBean.setMsg(HaierConstants.SUCCESS_VALUE);
            }else if (HaierConstants.TRADE_CLOSED.equals(result.get("tradelist") ) ||result.get("tradelist")==null){//失败
                resBean = ResponseBean.FAIL_RESPONSE();
               resBean.setMsg(result.get("memo"));
            }else {
                resBean = ResponseBean.WAIT_RESPONSE();
                resBean.setMsg(result.get("memo"));
            }
        }else {
            resBean = ResponseBean.WAIT_RESPONSE();
            resBean.setMsg(result.get("memo")==null?result.get("error_message"):result.get("memo"));
        }
        return resBean;
    }


}
